package com.dengqi.mymodule.activity.loader.album;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

import androidx.loader.content.CursorLoader;

/**
 * 图片加载器
 * Created by dell on 2019/5/7
 */
public class AlbumLoader extends CursorLoader {

    private static final Uri QUERY_URI = Uri.parse("content://media/external/file");

    private static final String[] PROJECTION = {
            "_id",
            "bucket_id",
            "bucket_display_name",
            "_data",
            "COUNT(*) AS " + "count"};

    private static final String SELECTION =
            "(media_type=? OR media_type =?) AND _size>0) GROUP BY (bucket_id";

    private static final String[] SELECTION_ARGS = {
            String.valueOf(MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE),
            String.valueOf(MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO)
    };

    private static final String BUCKET_ORDER_BY = "datetaken DESC";

    private AlbumLoader(Context context, String selection, String[] selectionArgs) {
        super(context, QUERY_URI, PROJECTION, SELECTION, SELECTION_ARGS, BUCKET_ORDER_BY);
    }

    public static CursorLoader newInstance(Context context) {
        String selection = SELECTION;
        String[] selectionArgs = SELECTION_ARGS;
        return new AlbumLoader(context, selection, selectionArgs);
    }

    @Override
    public Cursor loadInBackground() {
        return super.loadInBackground();
    }
}
